package com.marvels.product.param.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.dto.McptAmortMethodDTO;
import com.marvels.product.param.service.AmortMethodService;

/**
 * 还款计算方式
 * @author 宁华勇
 *
 */
@RestController
@RequestMapping("/product/param/amortmethod/service")
public class AmortMethodServiceController {

	@Autowired
	private AmortMethodService amortMethodService;
	
	@RequestMapping("list")
	public MarvelsResultDTO list(@RequestBody Map<String,Object> param){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(param));
		PageInfo<McptAmortMethodDTO> pageInfo = null;
		 try {
			pageInfo = amortMethodService.getPageInfoByParam(param);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), pageInfo);
	}
	
	
	@RequestMapping("save")
	public MarvelsResultDTO save(@RequestBody McptAmortMethodDTO dto,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			 UserDTO user = CacheUtils.getCacheUser();
			 String userName = user.getUserName();
			 dto.setCreatedBy(userName);
			amortMethodService.insert(dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), dto);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), dto);
	}
	
	
	@RequestMapping("getKey/{amortId}")
	public MarvelsResultDTO edit(@PathVariable Long amortId,HttpServletRequest request){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(amortId));
		McptAmortMethodDTO entity = null;
		try {
			 entity = amortMethodService.getEntityByPrimaryKey(amortId);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(e.getMessage());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), amortId);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), entity);
	}
	
	
	@RequestMapping("update")
	public MarvelsResultDTO update(@RequestBody McptAmortMethodDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			amortMethodService.updateByPrimaryKey(dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			MarvelsLogUtil.getInstance().warn(e.getMessage());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	@RequestMapping("updateStatus")
	public MarvelsResultDTO updateStatus(@RequestBody McptAmortMethodDTO dto){
		MarvelsLogUtil.getInstance().info(JSON.toJSONString(dto));
		try {
			amortMethodService.updateStatus(dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("系统异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().warn(e.getMessage());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
	}
	
	
	
	@RequestMapping("delete")
	@ResponseBody
	public MarvelsResultDTO delete(@RequestBody McptAmortMethodDTO dto){
		Integer count = 0;
		try {
			count = amortMethodService.delete(dto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error("删除还款计算方法异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("系统异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), count);
	}
	
	
	
	
}
